package a_o2odata_deal.utils.zhibo_zf

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/2/4 11:23
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object modify_zf_zhubo_v2 {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    //conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    //conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    //sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    for (months <- 1 to 1){
      val source_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/zhibo_zf/2020/${months}/zhubo_v1/")
      //println("处理前条数:"+source_data.count())

      source_data.createOrReplaceTempView("source_data")

      sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/taobao/2020/${months}/zhibo_finally")
        .selectExpr("userId","shopId","shopName","salesAmount","platformId").createOrReplaceTempView("tb_data")
      //取出uerrid对应销售额最多的shopId
      val taobao = sqlContext.sql(
        s"""
           |select
           |userId,
           |shopId,
           |shopName,
           |platformId
           |from
           |(select
           |*,
           |row_number() over(partition by userId order by cast(shop_sales as double) desc) as rank
           |from
           |(select
           |*,
           |sum(salesAmount) over(partition by shopId ) as shop_sales
           |from tb_data))
           |where rank=1
         """.stripMargin).dropDuplicates("userId")//.createOrReplaceTempView("taobao")


      sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/2020/${months}/zhibo_finally")
        .selectExpr("userId","shopId","shopName","salesAmount","platformId").createOrReplaceTempView("tm_data")
      val tmall = sqlContext.sql(
        s"""
           |select
           |userId,
           |shopId,
           |shopName,
           |platformId
           |from
           |(select
           |*,
           |row_number() over(partition by userId order by cast(shop_sales as double) desc) as rank
           |from
           |(select
           |*,
           |sum(salesAmount) over(partition by shopId ) as shop_sales
           |from tm_data))
           |where rank=1
         """.stripMargin).dropDuplicates("userId")//.createOrReplaceTempView("tmall")

      taobao.selectExpr("userId","shopId","shopName","platformId")
        .unionAll(tmall.selectExpr("userId","shopId","shopName","platformId")).createOrReplaceTempView("all")

      sqlContext.sql(
        s"""
           |select
           |*
           |from
           |(select
           |*,
           |row_number() over(partition by userId order by cast(platformId as long) desc) as rank
           |from all)
           |where rank=1
           """.stripMargin).drop("rank").dropDuplicates("userId")
        .createOrReplaceTempView("all_data")

      val result = sqlContext.sql(
        s"""
           |select
           |t1.*,
           |case when t2.userId is null then "" else t2.shopId end as shopId,
           |case when t2.userId is null then "" else t2.shopName end as shopName
           |from source_data t1
           |left join
           |all_data t2
           |on t1.anchorId=t2.userId
         """.stripMargin)

      //println("处理后条数:"+result.count())
      //result.show(false)

      result.repartition(4).write.orc(s"s3a://o2o-dataproces-group/panzonghao/zhibo_zf/2020/${months}/zhubo_v2/")
    }
  }

}
